﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class InventoryTracker_DropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            // This is because Table[1] contains Categories

            GridView1.DataSource = GetDataSet().Tables[0];

            GridView1.DataBind();

        }

    }

    private DataSet GetDataSet()
    {
//        string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p
//               SELECT c.CategoryID,c.CategoryName FROM Categories c";

        string query = @"SELECT ItemCode,ItemName FROM MaterialMaster ";
            
        
        string connectionString = @"Server=.\SQLEXPRESS;Database=F:\inv\App_Data\TimeTracker.mdf;;Trusted_Connection=true";
         
        SqlConnection myConnection = new SqlConnection(connectionString);

        SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);

        DataSet ds = new DataSet();

        ad.Fill(ds);

        return ds;

    }


    protected  void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

DataTable myTable = new DataTable();

DataColumn ItemNameColumn = new DataColumn("ItemName");

//DataColumn ItemDescriptionColumn = new DataColumn("ItemDescription");

myTable.Columns.Add(ItemNameColumn);

//myTable.Columns.Add(ItemDescriptionColumn);


DataSet ds = new DataSet();

ds = GetDataSet();

int ItemCode = 0;

string expression = String.Empty;

if (e.Row.RowType == DataControlRowType.DataRow)

{

ItemCode = Int32.Parse(e.Row.Cells[0].Text);

expression = "ItemCode = " + ItemCode;

DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");

DataRow[] rows = ds.Tables[0].Select(expression);

 

foreach (DataRow row in rows)

{

DataRow newRow = myTable.NewRow();
//newRow["ItemDescription"] = row["ItemDescription"];

newRow["ItemName"] = row["ItemName"];



myTable.Rows.Add(newRow);

}

ddl.DataSource = myTable;

ddl.DataValueField = "ItemCode";

ddl.DataTextField = "itemName";


ddl.DataBind();

}

}
}

